<?php


namespace App\Services;

use Illuminate\Support\Facades\Cache as CacheFile;
use Psr\SimpleCache\InvalidArgumentException;

class Cache
{
    //缓存的前缀
    private static $prefix = 'dailian_';

    /**
     * author:fhc
     * desc:获取Cache实例
     * created_at: 2020/7/21 002111:14
     */
    private static function getCache(): \Illuminate\Contracts\Cache\Repository
    {
        return CacheFile::store('file');
    }

    /**
     * author:fhc
     * desc:设置缓存的值
     * created_at: 2020/7/21 002111:15
     * @param $key
     * @param $value
     * @param null $ttl
     * @return bool
     * @throws InvalidArgumentException
     */
    public static function setCache($key,$value,$ttl=null){
        return self::getCache()->set(self::buildPrefix($key),$value,$ttl);
    }

    /**
     * @param $key
     * @return mixed
     * @throws InvalidArgumentException
     * author:fhc
     * desc:获取缓存的值
     * created_at: 2020/7/21 002111:17
     */
    public static function getCacheValue($key){
        return self::getCache()->get(self::buildPrefix($key));
    }

    /**
     * @param $key
     * author:fhc
     * desc:清除缓存
     * created_at: 2020/7/21 002113:15
     * @return bool
     */
    public static function cleanCache($key):bool {
        return self::getCache()->forget(self::buildPrefix($key));
    }

    /**
     * @param $key
     * @return bool
     * @throws InvalidArgumentException
     * author:fhc
     * desc:包含缓存
     * created_at: 2020/7/24-3:42 下午
     */
    public static function hasCache($key):bool {
        return self::getCache()->has(self::buildPrefix($key));
    }

    /**
     * @param string $key
     * author:fhc
     * desc:构建前缀
     * created_at: 2020/10/16{
     * @return string
     */
    private static function buildPrefix($key = ''):string {
        return self::$prefix.$key;
    }
}
